package com.vcredit.creditcat.web.app;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.net.util.Base64;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IAuthorizeManager;
import com.vcredit.creditcat.api.ICustIdAuthManager;
import com.vcredit.creditcat.api.IResourceManger;
import com.vcredit.creditcat.dto.UpdateUserNameDto;
import com.vcredit.creditcat.dto.identity.AuthIdentityDto;
import com.vcredit.creditcat.dto.identity.HandHeldCardDto;
import com.vcredit.creditcat.dto.identity.IdCardInfoDto;
import com.vcredit.creditcat.dto.resource.ResourceDto;
import com.vcredit.creditcat.enumtype.PhotoTypeEnum;
import com.vcredit.creditcat.enumtype.ResourceTypeEnum;
import com.vcredit.creditcat.exception.IdAuthException;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.FacePairOfAuth;
import com.vcredit.creditcat.vo.HandHeldCardPairOfAuth;
import com.vcredit.creditcat.vo.IdCardInfoVo;
import com.vcredit.creditcat.vo.ImageInfoOfAuth;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.creditcat.vo.SignatureVo;
import com.vcredit.framework.annotation.Log;

/**
 * @author zhangboshi
 *
 */
@RestController
@RequestMapping("/auth")
public class AuthenticationController extends BaseCreditCatController {

	@Reference(version = "1.0", timeout = 60000)
	private ICustIdAuthManager custIdAuthManager;
	@Reference(version = "1.0", group = "ftp")
	private IResourceManger resourceManger;

	@Reference(version = "1.0")
	private IAuthorizeManager authorizeManager;
	/**
	 * 身份认证初始化
	 * 
	 * @return
	 * @throws IdAuthException
	 */
	@Log
	@RequestMapping(value = "/getAuthInfo", method = RequestMethod.POST)
	public JsonResult<AuthIdentityDto> getAuthInfo(HttpServletRequest request) throws IdAuthException {
		Cust currentCust = super.getCust(request);
		AuthIdentityDto response = custIdAuthManager.findIdCardInfoByProcessId(currentCust);
		return JsonResult.SuccessResponse(response);
	}

	/**
	 * 图形解析(OCR识别) 拍照后进行图形解析
	 * 
	 * @param imageInfo
	 * @param request
	 * 
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/graphAnalysisORC")
	public JsonResult<IdCardInfoVo> graphAnalysisORC(@RequestBody @Valid ImageInfoOfAuth imageInfo, HttpServletRequest request)

			throws Exception {
		Cust currentCust = super.getCust(request);
		// 新增客户认证流程信息
		Long lastProcessId = currentCust.getLastProccessId();
		if (null == lastProcessId || 0 == lastProcessId) {
			throw new IdAuthException("请先注册信息");
		}
		// 证件上传
		ResourceDto resourceDto = null;
		if (PhotoTypeEnum.FONT.getCode().equals(imageInfo.getPhotoType())) {
			resourceDto = resourceManger.upload(ResourceTypeEnum.IMG, "idCardFront.jpg",
					Base64.decodeBase64(imageInfo.getIdImgData()));
		} else if (PhotoTypeEnum.BACK.getCode().equals(imageInfo.getPhotoType())) {
			resourceDto = resourceManger.upload(ResourceTypeEnum.IMG, "idCardBack.jpg",
					Base64.decodeBase64(imageInfo.getIdImgData()));
		} else {
			throw new IdAuthException("暂时未支持其他照片类型");
		}
		// 处理OCR解析及相关业务
		AuthIdentityDto authIdentityDto = null;
		IdCardInfoVo response = null;
		// 处理OCR解析及相关业务
		authIdentityDto = custIdAuthManager.saveIDcardImg(currentCust, imageInfo.getPhotoType(),
				imageInfo.getIdImgData(), resourceDto.getResourceCode());
		if (PhotoTypeEnum.FONT.getCode().equals(imageInfo.getPhotoType())) {
			if (null != authIdentityDto && authIdentityDto.getSame()) {
				return JsonResult
						.SuccessResponse(new IdCardInfoVo(authIdentityDto.getName(), authIdentityDto.getCitizenId(),
								authIdentityDto.getOriginalAccount(), authIdentityDto.getSame()));
			}
			response = new IdCardInfoVo(authIdentityDto.getName(), authIdentityDto.getCitizenId(),
					authIdentityDto.getAddress());
		} else if (PhotoTypeEnum.BACK.getCode().equals(imageInfo.getPhotoType())) {
			response = new IdCardInfoVo(authIdentityDto.getValidDateEnd());
		}
		return JsonResult.SuccessResponse(response);
	}

	/**
	 * 刷脸认证
	 * 
	 * @param imageInfo
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/verifyFacePair")
	public JsonResult<IdCardInfoDto> verifyFacePair(@RequestBody @Valid FacePairOfAuth imageInfo, HttpServletRequest request)
			throws Exception {
		Cust currentCust = super.getCust(request);

		// 照片相似度检测
		IdCardInfoDto response = custIdAuthManager.verifyFacePair(currentCust, imageInfo.getVerifyImgData());
		return JsonResult.SuccessResponse(response);
	}

	/**
	 * 身份证正面确认
	 * 
	 * @param request
	 * @param paramDto UpdateUserNameDto
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/confirmCardNo")
	public JsonResult<?> confirmCardNo(@RequestBody UpdateUserNameDto paramDto, HttpServletRequest request) throws Exception {
		Cust currentCust = super.getCust(request);
		Long accountId = super.getAccountId(request);
		custIdAuthManager.confirmCardNo(currentCust, accountId, paramDto);
		super.clearSession(request);
		return JsonResult.SuccessResponse(null);
	}
	
	/**
	 * 手写签名
	 * 
	 * @param request
	 * @param paramDto UpdateUserNameDto
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/saveSignature")
	public JsonResult<?> saveSignature(@RequestBody SignatureVo paramDto, HttpServletRequest request) throws Exception {				
		Cust currentCust = super.getCust(request);
		authorizeManager.signatureImgUpload(paramDto.getSignPhotoData(), currentCust.getCustId(), currentCust.getLastProccessId());		
		return JsonResult.SuccessResponse(null);
	}
	/**
	 * 手持身份证认证
	 * 
	 * @param imageInfo
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@Log
	@RequestMapping(value = "/handHeldCardPair")
	public JsonResult<HandHeldCardDto> handHeldCardPair(@RequestBody @Valid HandHeldCardPairOfAuth imageInfo, HttpServletRequest request)
			throws Exception {
		Cust currentCust = super.getCust(request);
		// 照片相似度检测
		HandHeldCardDto response = custIdAuthManager.handHeldPair(currentCust, imageInfo.getHandHeldIDCard());
		return JsonResult.SuccessResponse(response);
	}
}
